var express = require('express');
var router = express.Router();

/* GET users listing. */
const goodsApi = require('../api/shopGoods')

router.get('/', async function (req, res, next) {
  const { curPage, eachPage } = req.query
  const data = await goodsApi.getGoods({ curPage, eachPage })
  res.send(data);
});

router.post('/addGoods', async function (req, res, next) {
  const  goods  = req.body
  const a = await goodsApi.addGoods(goods)
  res.send(a)
});

router.patch("/updata", async function (req, res, next) {
  const goods = req.body;
  const data = await goodsApi.update(goods);
  res.send(data);
});
//删除商品
router.delete('/delgoods', async function (req, res, next) {
  const { _id } = req.query;
  await goodsApi.deleteGoods( _id );
  const data =await goodsApi.getGoods({ curPage:1, eachPage : 3 })

  res.send(data);
});
//查询商品
router.get('/serchgoods', async function (req, res, next) {
  const name = req.query;

  const data = await goodsApi.serchGoods(name)

  res.send(data);
});
//修改图片
router.patch('/updataImg', async function (req, res, next) {
 const {_id,images} = req.body.params
const data =  await goodsApi.updataImgs(_id,images)
  res.send(data)
});
module.exports = router;
